<template>
  <div>
    <!-- 权限点数据展示 -->
    <el-tree
      ref="refTree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="end">
      <el-button size="small" @click="hCancel">取消</el-button>
      <el-button size="small" type="primary" @click="hSave">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transDaraToTreeData } from '@/utils'
import { getRolesDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: [] // 存储权限数据
    }
  },
  created() {
    this.loadPermissionList()
    // console.log(this.roleId)
  },
  methods: {
    async hSave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      // console.log('当前选中的节点数组是', permIds)
      // const res = await assignPerm({
      await assignPerm({
        id: this.roleId,
        permIds
      })
      // console.log('保存角色的权限点的结果是', res)

      // 提示
      this.$message.success('保存角色的权限成功')
      // 关闭弹层
      this.hCancel()
      // console.log(111)
    },
    hCancel() {
      // 通过父组件去关闭弹层
      this.$emit('closeDialog')
      // console.log(222)

      // 清空当前的选择,用v-if，这里就不需要了
      // this.$refs.tree.setCheckedKeys([])
    },
    async loadPermissionList() {
    // 发送请求, 获取权限列表
      const { data } = await getPermissionList()
      // console.log('权限列表的数据是', data)
      this.permissionData = transDaraToTreeData(data)
      const res = await getRolesDetail(this.roleId)
      // console.log('获取当前角色的已有的权限点数据', res.data.permIds)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    }
  }

}
</script>

<style>

</style>
